//
//  StatusCell.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/19.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "StatusCell.h"
#import "Masonry.h"
#import "Status.h"
#import "StatusOriginalView.h"
#import "ReweetedStatusView.h"
#import "StatusPictureView.h"
#import "StatusCellBottomView.h"

#define StatusCellMargin 12
#define StatusCellIconWitdh 35

@interface StatusCell ()
@property (nonatomic, strong)StatusOriginalView *originalView;
@property (nonatomic, strong)StatusCellBottomView *bottomView;
@property (nonatomic, strong)ReweetedStatusView *retweetedView;
@property (nonatomic, strong)StatusPictureView *pictureView;
//原创微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
@property (nonatomic, strong)MASConstraint *bottomConstraints;
@end

@implementation StatusCell


- (void)setStatus:(Status *)status {
    //设置原创微博的数据模型
    self.originalView.status = status;
    
    // 移除约束
    [self.bottomConstraints uninstall];
    //设置转发微博的数据模型
    Status *re = status.retweeted_status;
    if (re != nil) {
        //设置数据源
        self.retweetedView.retweetedStatus = re;
        //设置显示
        self.retweetedView.hidden = false;
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.top.equalTo(self.retweetedView.mas_bottom);
        }];
    } else {
        //设置隐藏
        self.retweetedView.hidden = true;
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.top.equalTo(self.originalView.mas_bottom);
        }];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setupUI];
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    return self;
}

// MARK: 3.重写父类的构造方法 调用设置 UI
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    [self setupUI];
    
    return self;
}


// MARK: 2.设置 UI
- (void)setupUI {
    // 添加子控件
    [self.contentView addSubview:self.originalView];
    [self.contentView addSubview:self.retweetedView];
    [self.contentView addSubview:self.bottomView];
    
    //设置约束信息
    [self.originalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
    }];
    [self.retweetedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.originalView.mas_bottom);
        make.left.equalTo(self.originalView.mas_left);
        make.right.equalTo(self.originalView.mas_right);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        self.bottomConstraints = make.top.equalTo(self.retweetedView.mas_bottom);
        make.right.equalTo(self.contentView.mas_right);
        make.height.mas_equalTo(40);
    }];
    
    //容器视图的底部约束 是自动计算行高关键
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        //contentView 底部约束 是最关键的一点
        make.bottom.equalTo(self.bottomView.mas_bottom);
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
    }];
}

// MARK: 1.懒加载所有子控件
- (StatusOriginalView *)originalView {
    if (_originalView == nil) {
        _originalView = [[StatusOriginalView alloc] init];
    }
    return _originalView;
}

- (StatusCellBottomView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[StatusCellBottomView alloc] init];
    }
    return _bottomView;
}

- (ReweetedStatusView *)retweetedView {
    if (_retweetedView == nil) {
        _retweetedView = [[ReweetedStatusView alloc] init];
    }
    return _retweetedView;
}

- (StatusPictureView *)pictureView {
    if (_pictureView == nil) {
        _pictureView = [[StatusPictureView alloc] init];
    }
    return _pictureView;
}
@end
